/**
 * * Copyright &copy; 2015-2020 <a href="https://gitee.com/JeeHuangBingGui/JeeSpring">JeeSpring</a> All rights reserved..
 */
package com.jeespring.modules.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeespring.common.utils.StringUtils;
import com.jeespring.common.web.AbstractBaseController;
import com.jeespring.modules.sys.entity.Menu;
import com.jeespring.modules.sys.service.SysConfigService;
import com.jeespring.modules.sys.service.SystemService;
import com.jeespring.modules.sys.utils.UserUtils;

/**
 * 菜单Controller
 *
 * @author 黄炳桂 516821420@qq.com
 * @version 2013-3-23
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/menu")
public class MenuController extends AbstractBaseController {

    @Autowired
    private SystemService systemService;
    @Autowired
    private SysConfigService sysConfigService;

    @ModelAttribute("menu")
    public Menu get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return systemService.getMenu(id);
        } else {
            return new Menu();
        }
    }

    // RequiresPermissions("sys:menu:list")
    @RequestMapping(value = {"list", ""})
    public String list(Model model, RedirectAttributes redirectAttributes) {
        List<Menu> list = Lists.newArrayList();
        List<Menu> sourcelist = systemService.findAllMenu();
        // if(sysConfigService.isDemoMode()){
        // model.addAttribute("list", list);
        // model.addAttribute("message", sysConfigService.isDemoModeDescription());
        // return "modules/sys/menuList";
        // }else{
        Menu.sortList(list, sourcelist, Menu.getRootId(), true);
        // }
        model.addAttribute("list", list);
        return "modules/sys/menuList";
    }

    // RequiresPermissions(value={"sys:menu:view","sys:menu:add","sys:menu:edit"},logical=Logical.OR)
    @RequestMapping(value = "form")
    public String form(Menu menu, Model model, HttpServletRequest request) {
        if (menu.getParent() == null || menu.getParent().getId() == null) {
            menu.setParent(new Menu(Menu.getRootId()));
        }
        menu.setParent(systemService.getMenu(menu.getParent().getId()));
        // 获取排序号，最末节点排序号+30
        if (StringUtils.isBlank(menu.getId())) {
            List<Menu> list = Lists.newArrayList();
            List<Menu> sourcelist = systemService.findAllMenu();
            Menu.sortList(list, sourcelist, menu.getParentId(), false);
            if (!list.isEmpty()) {
                menu.setSort(list.get(list.size() - 1).getSort() + 30);
            }
        }
        model.addAttribute("action", request.getParameter("action"));
        model.addAttribute("menu", menu);
        return "modules/sys/menuForm";
    }

    // RequiresPermissions(value={"sys:menu:add","sys:menu:edit"},logical=Logical.OR)
    @RequestMapping(value = "save")
    public String save(Menu menu, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        if (!UserUtils.getUser().isAdmin()) {
            addMessage(redirectAttributes, "越权操作，只有超级管理员才能添加或修改数据！");
            return "redirect:" + adminPath + "/sys/role/?repage";
        }
        if (sysConfigService.isDemoMode()) {
            addMessage(redirectAttributes, sysConfigService.isDemoModeDescription());
            return "redirect:" + adminPath + "/sys/menu/";
        }
        if (!beanValidator(model, menu)) {
            return form(menu, model, request);
        }
        systemService.saveMenu(menu);
        addMessage(redirectAttributes, "保存菜单'" + menu.getName() + "'成功");
        return "redirect:" + adminPath + "/sys/menu/";
    }

    // RequiresPermissions("sys:menu:del")
    @RequestMapping(value = "delete")
    public String delete(Menu menu, RedirectAttributes redirectAttributes) {
        if (sysConfigService.isDemoMode()) {
            addMessage(redirectAttributes, sysConfigService.isDemoModeDescription());
            return "redirect:" + adminPath + "/sys/menu/";
        }
//        if (Menu.isRoot(id)){
//            addMessage(redirectAttributes, "删除菜单失败, 不允许删除顶级菜单或编号为空");
//        }else{
        systemService.deleteMenu(menu);
        addMessage(redirectAttributes, "删除菜单成功");
//        }
        return "redirect:" + adminPath + "/sys/menu/";
    }

    // RequiresPermissions("sys:menu:del")
    @RequestMapping(value = "deleteAll")
    public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
        if (sysConfigService.isDemoMode()) {
            addMessage(redirectAttributes, sysConfigService.isDemoModeDescription());
            return "redirect:" + adminPath + "/sys/menu/";
        }
//        if (Menu.isRoot(id)){
//            addMessage(redirectAttributes, "删除菜单失败, 不允许删除顶级菜单或编号为空");
//        }else{
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            Menu menu = systemService.getMenu(id);
            if (menu != null) {
                systemService.deleteMenu(systemService.getMenu(id));
            }
        }

        addMessage(redirectAttributes, "删除菜单成功");
//        }
        return "redirect:" + adminPath + "/sys/menu/";
    }

    // RequiresPermissions("user")
    @RequestMapping(value = "tree")
    public String tree() {
        return "modules/sys/menuTree";
    }

    // RequiresPermissions("user")
    @RequestMapping(value = "treeselect")
    public String treeselect(String parentId, Model model) {
        model.addAttribute("parentId", parentId);
        return "modules/sys/menuTreeselect";
    }

    /**
     * 批量修改菜单排序
     */
    // RequiresPermissions("sys:menu:updateSort")
    @RequestMapping(value = "updateSort")
    public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
        if (sysConfigService.isDemoMode()) {
            addMessage(redirectAttributes, sysConfigService.isDemoModeDescription());
            return "redirect:" + adminPath + "/sys/menu/";
        }
        for (int i = 0; i < ids.length; i++) {
            Menu menu = new Menu(ids[i]);
            menu.setSort(sorts[i]);
            systemService.updateMenuSort(menu);
        }
        addMessage(redirectAttributes, "保存菜单排序成功!");
        return "redirect:" + adminPath + "/sys/menu/";
    }

    /**
     * isShowHide是否显示隐藏菜单
     *
     * @param extId
     * @param isShowHidden
     * @param response
     * @return
     */
    // RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId,
                                              @RequestParam(required = false) String isShowHide, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<Menu> list = systemService.findAllMenu();
        for (int i = 0; i < list.size(); i++) {
            Menu e = list.get(i);
            if (StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId())
                    && e.getParentIds().indexOf("," + extId + ",") == -1)) {
                if (isShowHide != null && "0".equals(isShowHide) && "0".equals(e.getIsShow())) {
                    continue;
                }
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("name", e.getName());
                mapList.add(map);
            }
        }
        return mapList;
    }
}
